﻿using System;
using System.Diagnostics;
using Microsoft.Coding4Fun.TweetCraft.Tests.Properties;
using Microsoft.Coding4Fun.TweetCraft.Twitter;

using NUnit.Framework;

namespace Microsoft.Coding4Fun.TweetCraft.Tests
{
    [TestFixture]
    [Category("Integration")]
    public class TwitterIntegrationTest
    {
        private ITwitterClient twitterClient;

        [SetUp]
        public void SetUp()
        {
            // Initialize TwitterNet
            twitterClient = new TwitterClient(Settings.Default.TwitterToken, Settings.Default.TwitterTokenSecret.ToSecureString());
        }

        [Test]
        public void GetRequestTokenTest()
        {
            var token = twitterClient.GetRequestToken();
            Assert.IsNotNull(token);
            Assert.AreEqual(2, token.Length);
        }

        [Test]
        [Ignore("This is a non-sequential test requiring a debugger")]
        public void GetAccessTokenTest()
        {
            var token = twitterClient.GetRequestToken();
            Assert.IsNotNull(token);
            Assert.AreEqual(2, token.Length);

            var url = "https://api.twitter.com/oauth/authorize?oauth_token=" + token[0];
            Process.Start(url);

            var verifier = "123456"; // <-- Breakpoint the next line and change this to the valid PIN
            
            token = twitterClient.GetAccessToken(token, verifier);
            Assert.IsNotNull(token);
            Assert.AreEqual(2, token.Length);

            Trace.WriteLine(token[0] + "," + token[1]);
        }

        [Test]
        public void GetFriendsTimelineTest()
        {
            Tweet[] tweets = twitterClient.GetFriendsTimeline();
            Assert.IsNotNull(tweets);
            Assert.IsTrue(tweets.Length > 0);
        }

        [Test]
        public void GetMentionsTest()
        {
            Tweet[] tweets = twitterClient.GetMentions();
            Assert.IsNotNull(tweets);
            if(tweets.Length == 0)
            {
                Assert.Ignore("This user may have no mentions legitimately.");
            }
            Assert.IsTrue(tweets.Length > 0);
        }

        [Test]
        public void AddTweetTest()
        {
            Tweet tweet = twitterClient.AddTweet("TwitterIntegrationTest:AddTweetTest:" + DateTime.Now.Ticks); // <-- Avoid duplicate tweet errors
            Assert.IsNotNull(tweet);
            Assert.IsNotNull(tweet.User);
            Assert.AreNotEqual(0, tweet.Id);
            Assert.IsFalse(String.IsNullOrEmpty(tweet.Message));
        }

        [Test]
        public void UploadAndPostTest()
        {
            Tweet twitPicTweet = twitterClient.UploadAndPost("rgabostyle.jpg", "TwitterIntegrationTest:UploadAndPostTest");
            Assert.IsNotNull(twitPicTweet);
            Assert.IsNotNull(twitPicTweet.User);
            Assert.AreNotEqual(0, twitPicTweet.Id);
            Assert.IsFalse(String.IsNullOrEmpty(twitPicTweet.Message));
            Assert.IsTrue(twitPicTweet.Message.Contains("http://twitpic.com"));
        }

        [Test]
        public void ValidateAccountTest()
        {
            Assert.IsTrue(TwitterClient.ValidateAccount(Settings.Default.TwitterToken, Settings.Default.TwitterTokenSecret.ToSecureString()));
        }

        [Test]
        public void ValidateInvalidAccountTest()
        {
            Assert.IsFalse(TwitterClient.ValidateAccount(Settings.Default.TwitterToken, "bogus".ToSecureString()));
        }

    }
}
